package o.gwtp.editor.client.navigation;

import o.gwtp.editor.client.navigation.NavigationTabPresenter.MyView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.Widget;
import com.gwtplatform.mvp.client.Controls;
import com.gwtplatform.mvp.client.ViewWithControls;

/**
 * @author Christian Goudreau
 */
public class NavigationTabView extends ViewWithControls<NavigationTabView.MyControls> implements MyView {
  private static Binder uiBinder = GWT.create(Binder.class);
  interface Binder extends UiBinder<Widget, NavigationTabView> {
  }
  
  public interface MyControls extends Controls {
    void onNewRequest(String name);
    void onTabSelected(int intex);
  }
  
  private final Widget widget;
  
  @UiField
  Button newButton;
  @UiField
  TabBar tabBar;

  public NavigationTabView() {
    widget = uiBinder.createAndBindUi(this);
  }

  @Override
  public Widget asWidget() {
    return widget;
  }

  @Override
  public void createTab(Widget tabElement) {
    tabBar.addTab(tabElement);
  }

  @Override
  public void removeTab(int index) {
    tabBar.removeTab(index);
  }

  @Override
  public void selectTab(int index) {
    tabBar.selectTab(index);    
  }

  @UiHandler("newButton")
  void onNewButtonClicked(ClickEvent event) {
    String name = Window.prompt("Choose a name", "");
    
    if (name != null) {
      getControls().onNewRequest(name);
    }
  }
  
  @UiHandler("tabBar")
  void onTabBarSelection(SelectionEvent<Integer> event) {
    getControls().onTabSelected(event.getSelectedItem());
  }
}
